/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TComment;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OrderAsksModel;
import com.ruipengkj.service.TCommentService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.memberView.MemberAsks;
/**
 * 订单评论
 */
public class OrderAsksPanel extends BasePagePanel<TComment> implements Runnable{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent orderNum; //咨询编号
	
	private JTable table; //订单Table
	
	private JButton queryButton; //查询按钮
	private JButton cancelButton;//取消按钮
	private OrderAsksModel orderAsksModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TComment> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TCommentService tCommentService;
	private MemberAsks memberAsks;//评论回复
	public OrderAsksPanel() {
		
		setLayout(new BorderLayout());
		init();
		initListener();
		tCommentService=BuildSpringApplication.getInstance().getTCommentServiceImpl();
	}
	
	private void initListener() {
		// TODO Auto-generated method stub
		queryButton.addActionListener(this);
		cancelButton.addActionListener(this);
	}
	public void init() {
		pageView=new PageView<TComment>();
		orderAsksModel=new OrderAsksModel();
		table=uiFactory.createRPJTable(orderAsksModel, null, this, 50, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		
		pagingComponent.setPageDelegale(this);
		this.add(topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(pagingComponent,BorderLayout.SOUTH);
		//排序
		TableRowSorter<OrderAsksModel> tableRowSorter = new TableRowSorter<OrderAsksModel>(orderAsksModel);  
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel(){
		QueryPanel queryPanel=new QueryPanel("咨询回复",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_1));
		JPanel top_jp=new JPanel();
		top_jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		orderNum = new JLabelJTextFieldComponent("订单号：", 15);
		queryButton = new RPJButtonBlue("查询");
		cancelButton= new RPJButtonGray("清空");
		
		top_jp.add(orderNum);
		top_jp.add(queryButton);
		top_jp.add(cancelButton);
		queryPanel.addPanel(top_jp);
		return queryPanel;
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
			
		}else if(e.getSource()==cancelButton){
			//清除
			orderNum.getText().setText("");
		}
	}
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TComment tComment=new TComment();
		tComment.setStatus((byte)10);
		tComment.setCommentID(orderNum.getText().getText());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TComment> pageModel=tCommentService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tComment);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						orderAsksModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						orderAsksModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	@Override
	protected PageView<TComment> getPageView() {
		return pageView;
	}
	
	// 鼠标双击 显示详细
	 
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), -1).toString();
    		try {
    			TComment tComment=tCommentService.getByIdService(id);
				if(tComment==null){
					JOptionPane.showMessageDialog(null, "该订单暂未评价！");
					return;
				}
				memberAsks=new MemberAsks(null, "咨询回复", true);
				memberAsks.showDialog(tComment);
				if(memberAsks.isSuccess==true){
					this.termQuery(1);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
